package com.hn658.user.sync.department;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.hn658.framework.shared.utils.JsonMapper;
import com.hn658.user.sync.Env;
import com.hn658.user.sync.auth.AuthHelper;
import com.hn658.user.sync.exception.OApiException;
import com.hn658.user.sync.httpClient.HttpHelper;
import com.hn658.user.sync.httpClient.HttpRequest;
import com.hn658.user.sync.httpClient.HttpResponse;

/**
 * 钉钉部门获取API
 * 
 * @author lsj
 *
 */
public class DepartmentHelper {

	private static JsonMapper mapper = JsonMapper.nonEmptyMapper();

	/**
	 * 获取部门列表
	 * 
	 * @param accessToken
	 * @return
	 * @throws OApiException
	 */
	public static List<Department> listDepartments() throws OApiException {
		String accessToken = AuthHelper.getAccessToken();
		HttpRequest httpRequest = new HttpRequest(null);
		String url = Env.OAPI_HOST + "/department/list?" + "access_token="
				+ accessToken;
		httpRequest.setUrl(url);
		httpRequest.setMethod(HttpRequest.METHOD_GET);
		HttpResponse response = HttpHelper.httpGet(httpRequest);
		String stringResult = response.getStringResult();
		DepartmentListResponse departmentResponse = mapper.fromJson(stringResult,
				DepartmentListResponse.class);
		if (departmentResponse != null
				&& departmentResponse.getDepartment() != null) {
			return departmentResponse.getDepartment();
		}
		return null;
	}

	/**
	 * 获取部门详情
	 * 
	 * @param accessToken
	 * @param id
	 *            部门id
	 * @return
	 * @throws OApiException
	 */
	public static Department departmentsDetail(Long id) throws OApiException {
		String accessToken = AuthHelper.getAccessToken();
		HttpRequest httpRequest = new HttpRequest(null);
		String url = Env.OAPI_HOST + "/department/get?" + "access_token="
				+ accessToken + "&id=" + id;
		httpRequest.setUrl(url);
		httpRequest.setMethod(HttpRequest.METHOD_GET);
		HttpResponse response = HttpHelper.httpGet(httpRequest);
		String stringResult = response.getStringResult();
		Department department = mapper.fromJson(stringResult, Department.class);
		return department;
	}

	/**
	 * 创建部门
	 * 
	 * @param accessToken
	 * @param name
	 * @param parentId
	 * @param order
	 * @param createDeptGroup
	 * @return
	 * @throws OApiException
	 * @throws UnsupportedEncodingException
	 */
	public static long createDepartment(String name, String parentId,
			String order, boolean createDeptGroup) {
		String accessToken = AuthHelper.getAccessToken();
		HttpRequest httpRequest = new HttpRequest(null);
		String url = Env.OAPI_HOST + "/department/create?" + "access_token="
				+ accessToken;
		Map<String, Object> args = new HashMap<String, Object>();
		httpRequest.setUrl(url);
		httpRequest.setMethod(HttpRequest.METHOD_POST);
		args.put("name", name);
		args.put("parentid", parentId);
		args.put("order", order);
		args.put("createDeptGroup", createDeptGroup);
		args.put("autoAddUser", true);
		HttpResponse httpGetOrPost = HttpHelper.httpPost(httpRequest, args);
		String stringResult = httpGetOrPost.getStringResult();
		DepartmentResponse response = mapper.fromJson(
				stringResult, DepartmentResponse.class);
		if(response.getErrcode()!=0){
			throw new OApiException(String.valueOf(response.getErrcode()), response.getErrmsg(), new String[]{});
		}
		return response.getId();
	}

	/**
	 * 更新部门
	 * 
	 * @param accessToken
	 * @param name
	 * @param parentId
	 * @param order
	 * @param id
	 * @param autoAddUser
	 * @throws OApiException
	 */
	public static DepartmentResponse updateDepartment(Department department) throws OApiException {
		String accessToken = AuthHelper.getAccessToken();
		HttpRequest httpRequest = new HttpRequest(null);
		String url = Env.OAPI_HOST + "/department/update?" + "access_token="
				+ accessToken;
		Map<String, Object> args = new HashMap<String, Object>();
		args.put("name", department.getName());
		args.put("parentid",department.getParentid());
		args.put("order", department.getOrder());
		args.put("id", department.getId());
		args.put("autoAddUser",department.getAutoAddUser());
		httpRequest.setUrl(url);
		httpRequest.setMethod(HttpRequest.METHOD_POST);
		HttpResponse httpPost = HttpHelper.httpPost(httpRequest, args);
		String stringResult = httpPost.getStringResult();
		DepartmentResponse response = mapper.fromJson(
				stringResult, DepartmentResponse.class);
		if(response.getErrcode()!=0){
			throw new OApiException(String.valueOf(response.getErrcode()), response.getErrmsg(), new String[]{});
		}
		return response;
	}

	/**
	 * 删除部门
	 * 
	 * @param accessToken
	 * @param id
	 * @throws OApiException
	 */
	public static DepartmentResponse deleteDepartment(Long id)
			throws OApiException {
		String accessToken = AuthHelper.getAccessToken();
		HttpRequest httpRequest = new HttpRequest(null);
		String url = Env.OAPI_HOST + "/department/delete?" + "access_token="
				+ accessToken + "&id=" + id;
		httpRequest.setUrl(url);
		httpRequest.setMethod(HttpRequest.METHOD_GET);
		HttpResponse httpGet = HttpHelper.httpGet(httpRequest);
		String stringResult = httpGet.getStringResult();
		DepartmentResponse response = mapper.fromJson(
				stringResult, DepartmentResponse.class);
		if(response.getErrcode()!=0){
			throw new OApiException(String.valueOf(response.getErrcode()), response.getErrmsg(), new String[]{});
		}
		return response;
	}
}
